package linkedlist.exercise;


import java.util.HashSet;
import java.util.LinkedHashSet;

/**
 * LeetCode 206，141，21，19，876
 * @date 2021-06-21
 * @author Administrator
 */
public class June_TwentyOne {
    public static void main(String[] args) {
        ListNode node = new ListNode(5);
        ListNode node2 = new ListNode(4);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(2);
        ListNode node5 = new ListNode(1);
        node.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        System.out.println(reverseList(node));
    }

    public static ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }

        ListNode current = head;
        ListNode tmp = head.next;
        ListNode pre;
        while(tmp != null) {

            pre = current;
            current = tmp;
            tmp = current.next;
            current.next = pre;
        }

        return current;
    }

    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

}
